package com.tsd.jxc.stock.service.impl;

import com.tsd.common.vo.BatchDeleteLogicVo;
import com.tsd.core.constants.VersionConstants;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.utils.ListUtil;
import com.tsd.core.vo.ErrorCode;
import com.tsd.core.vo.HlpException;
import com.tsd.core.vo.HttpResult;
import com.tsd.core.vo.PageBean;
import com.tsd.jxc.stock.dao.JxcStockWarehouseMapper;
import com.tsd.jxc.stock.entity.JxcStockWarehouse;
import com.tsd.jxc.stock.entity.JxcStockWarehouseExt;
import com.tsd.jxc.stock.service.JxcStockWarehouseService;
import com.tsd.jxc.utils.TreeUtil;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.entity.SysUser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 描述：JxcStockWarehouse 服务实现层
 *
 * @author Hillpool
 * @date 2022/07/29 15:06:33
 */
@Service
public class JxcStockWarehouseServiceImpl extends BaseServiceImpl implements JxcStockWarehouseService {

    @Resource
    private JxcStockWarehouseMapper jxcStockWarehouseMapper;

    @Override
    public JxcStockWarehouse getJxcStockWarehouseById(Long id) throws Exception {
        return jxcStockWarehouseMapper.selectByPrimaryKey(id);
    }

    @Override
    public JxcStockWarehouseExt queryBySid(String sid) throws Exception {
        return jxcStockWarehouseMapper.selectBySid(sid);
    }

    @Override
    public void deleteJxcStockWarehouses(List<String> list, SysUser opUser) throws Exception {
        JxcStockWarehouseExt params = new JxcStockWarehouseExt();
        params.setDr(0);
        params.setSid_list(list);
        List<JxcStockWarehouseExt> warehouseExtList = jxcStockWarehouseMapper.selectByEntity(params);
        if (HlpUtils.isEmptyList(warehouseExtList)) {
            return;
        }
        List<String> sidList = ListUtil.map(warehouseExtList, JxcStockWarehouseExt::getSid);
        BatchDeleteLogicVo logicVo = new BatchDeleteLogicVo(opUser);
        logicVo.setSidList(sidList);
        jxcStockWarehouseMapper.deleteLogicBySidList(logicVo);
    }

    @Override
    public HttpResult<String> saveJxcStockWarehouse(JxcStockWarehouse record, SysUser opUser) throws Exception {
        this.validateRepeat(record);
        if (HlpUtils.isEmpty(record.getId())) {
            String accountSetVersion = VersionConstants.getAccountSetVersion();
            Integer accountSetVersionInt = VersionConstants.getVersionValue(accountSetVersion);
            int validQty = jxcStockWarehouseMapper.countValid();
            if (validQty == 0 || accountSetVersionInt >= VersionConstants.VERSION_VALUE_PRO) {
                super.autoInjectBaseData(record, opUser, this.TYPE_CREATE);
                if (HlpUtils.isEmpty(record.getUsed_ratio())) {
                    record.setUsed_ratio(JxcStockWarehouseExt.DEFAULT_USED_RATIO);
                }
                jxcStockWarehouseMapper.insertSelective(record);
            }else{
                return HttpResult.fail(ErrorCode.CODE_MISMATCH_VERSION,"当前版本只支持一个仓库");
            }
        } else {
            super.autoInjectBaseData(record, opUser, this.TYPE_UPDATE);
            jxcStockWarehouseMapper.updateByPrimaryKeySelective(record);
        }
        return null;
    }

    /**
     * 判断code、name是否重复
     *
     * @param record
     */
    private void validateRepeat(JxcStockWarehouse record) throws HlpException {
        List<JxcStockWarehouseExt> list = jxcStockWarehouseMapper.validateRepeatJxcStockWarehouses(record);
        if (list.size() > 0) {
            List<JxcStockWarehouseExt> codeRepeatList = ListUtil.filter(list, t -> t.getCode().equals(record.getCode()));
            if (codeRepeatList.size() > 0) {
                throw new HlpException("编号重复！");
            } else {
                throw new HlpException("名称重复！");
            }
        }
    }

    @Override
    public List<JxcStockWarehouseExt> findJxcStockWarehouses(Map<String, Object> params, PageBean page) throws Exception {
        return jxcStockWarehouseMapper.selectByPage(params, page);
    }

    @Override
    public List<JxcStockWarehouseExt> findJxcStockWarehouses(Map<String, Object> params) throws Exception {
        return jxcStockWarehouseMapper.selectByParams(params);
    }

}
